package com.example.yinhangmiaosha.filter;

import org.springframework.core.annotation.Order;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 单用户频率访问拦截
 * 5s内做出5次响应
 */
//@WebFilter(filterName = "IPFilter", urlPatterns = "/*")
@Order(2)
public class IPFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("init");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        String ip = request.getRemoteAddr();
        System.out.println("ip："+ip);
//        User user = (User) SecurityUtils.getSubject().getPrincipal();
//        System.out.println("过滤器中用户："+user);
        filterChain.doFilter(request,response);
    }

    @Override
    public void destroy() {

    }
}
